import numpy as np
import matplotlib.pyplot as plt
import sys
import caffe

def Analyze_Net_Structure(md,mw,mm):
    net_structure=[]
    plt.rcParams['figure.figsize'] = (10,10)
    plt.rcParams['image.interpolation'] = 'nearest'
    plt.rcParams['image.cmap'] = 'gray'
    caffe_root = 'C:/Program Files/caffe/caffe-master/caffe-master'
    sys.path.insert(0, caffe_root+'python')
    caffe.set_mode_cpu()
    model_def=md
    model_weights=mw
    mean_filename=mm
    net = caffe.Net(model_def, model_weights, caffe.TEST)
    transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2,0,1))  
    # transformer.set_mean('data', mean)
    transformer.set_mean('data',np.load(mean_filename).mean(1).mean(1))
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2,1,0))
    net.blobs['data'].reshape(1,3,227,227)
    print 'The structure of the NET is shown as following:'
    for layer_name, blob in net.blobs.iteritems():
        net_structure.append(layer_name+'\t'+str(blob.data.shape))
    return net_structure

#How to use:
# config_root='F:/face-recognition/age_gender/'
# model_def = config_root+'deploy_gender.prototxt'
# model_weights = config_root+'gender_net.caffemodel'
# mean_filename = config_root+'mean.npy'
# ns=Analyze_Net_Structure(model_def,model_weights,mean_filename)
# for item in ns:
#     print item

